home *** CD-ROM | disk | FTP | other *** search
- Path: hubcap.clemson.edu!hubcap!mjs
- From: mjs@hubcap.clemson.edu (M. J. Saltzman)
- Newsgroups: comp.lang.c
- Subject: Re: switching array pointers query
- Date: 1 Jan 96 16:51:09 GMT
- Organization: Clemson University
- Message-ID: <mjs.820515069@hubcap>
- References: <95123120361029516@tglbbs.com>
- NNTP-Posting-Host: hubcap.clemson.edu
-
- charles.herold@tglbbs.com (Charles Herold) writes:
-
- >My struggle with arrays of pointers and such not continues, and here's
- >an example of my difficulties. Below is a bit of code that works, after
- >trying various ways that didn't work. However, it doesn't do what I
- >want quite the way I intended. I have an array of pointers to
- >structures, and wish to simply reverse the order of the array. What I
- >*want* is to simply change what the pointers point to, that is, the
- >first pointer points to what begins as the last element of the array,
- >the last points to the first, and so on. But I couldn't get that, and
- >you see below that I am simply moving the information in the last
- >element into where the first pointer is pointing etc. Since it
- >does work, it might seem like I could just not worry about it. But I'm
- >hoping seeing how this should be done might give me an understanding
- >breakthrough on this whole pointer to array thing that drives me crazy.
-
- >Thanks to all who help.
-
- >void
- >ReverseFiles( struct find_t **files, int numberfiles )
-
- /* files is a pointer to the first element of an array of pointers
- to structs. So to refer to the i'th pointer in this array, you will
- want to write "files[i]", and to refer to the struct it points to, you
- will write "*files[i]". */
-
- >{
- > int i, h;
- > struct find_t temp;
-
- /* since you want to swap struct pointers, your temp should be a
- struct pointer
-
- struct find_t *temp;
- */
-
- > for( i = 0, h = numberfiles - 1; i < h; i++, h-- ) {
- > temp = (*files)[i];
- > (*files)[i] = (*files)[h];
- > (*files)[h] = temp;
-
- /* now, your swap should be
-
- temp = files[i];
- files[i] = files[h];
- files[h] = temp;
-
- */
-
- > }
- >}
-
- Let's take a moment to look at what you wrote, though, because it's
- instructive to see why it appears to work. (By "work" here, I mean
- reorder the structs themselves--as you have correctly reasoned or
- discovered that it does.)
-
- The expression (*files)[i] is evaluated as follows:
-
- *files dereferences the pointer files, which is pointing
- to the first element in the array of pointers. Let's store
- this value in a temporary, say
-
- struct find_t *x = *files;
-
- Now we want to know about x[i]. This is evaluated as
- *(x + i): it's value is the value of the i'th struct
- past the one pointed to by x, in an array of structs.
-
- Now, x *happens* to be pointing to the first struct in your array of
- structs, so x[i] (hence (*files)[i]) *happens* to refer to the i'th
- struct, and the code succeeds in physically reversing the elements of
- the array of structs. But if the first pointer in the list *happened*
- to point to a different element (or if the structs were not stored in
- an array at all, but were malloc'ed independently), this wouldn't work
- at all: at least some of the array references would be outside the
- legal range. Furthermore, if *any* of the pointers pointed out of
- sequence, this would probably not do what you expected. Compare it to
-
- temp = *files[i];
- *files[i] = *files[h];
- *files[h] = temp;
-
- which swaps the struct pointed to by the i'th and h'th pointers. So
- this version moves the struct pointed to by files[0] to the location
- pointed to by files[numberfiles - 1], etc., no matter in what sequence
- the pointers referred to the structs (or even if the structs weren't
- stored in an array).
-
- Try tracing through the results of running each of these versions
- twice on a short array (reversing and reversing again), to see how
- the three versions behave differently.
- --
- Matthew Saltzman
- Clemson University Math Sciences
- mjs@clemson.edu
-